package com.kaishengit.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Comment;
import com.kaishengit.entity.Document;
import com.kaishengit.entity.DocumentComment;

import com.kaishengit.entity.User;
import com.kaishengit.service.CommentService;
import com.kaishengit.service.DocumentService;
import com.kaishengit.service.MessageService;
import com.kaishengit.util.Page;


public class DocumentServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private DocumentService service = new DocumentService();
	private CommentService cs = new CommentService();
	private MessageService ms = new MessageService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if(method == null || "".equals(method)){
			findAllDocument(request,response);
		}else if("toDocument".equals(method)){
			toDocumentShow(request,response);
		}else if("toNewDocument".equals(method)){
			String httpmethod = request.getMethod();
			if(httpmethod.equals("POST")){
				NewDocument(request,response);
			}
			if(httpmethod.equals("GET")){
				toNewDocument(request,response);
			}
			
		}else if("edit".equals(method)){
			String httpmethod = request.getMethod();
			if(httpmethod.equals("POST")){
				edit(request,response);
			}
			if(httpmethod.equals("GET")){
				toEdit(request,response);
			}
		}else if("del".equals(method)){
			todel(request,response);
		}else{
			response.sendError(404);
		}	
		
	}
	private void todel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		ms.delById(id);
		cs.delByDocumentId(id);
		service.del(id);
		response.sendRedirect("document.jspx");
		
	}
	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Document document = service.findDocument(id,user.getId());
		if(document == null ){
			response.sendError(403,"你无权编辑此资料！");
			return;	
		}
		request.setAttribute("mydocument", document);
		request.getRequestDispatcher("WEB-INF/views/edit_document.jsp").forward(request, response);
		
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id =request.getParameter("id");
		String title = request.getParameter("title");
		String content = request.getParameter("dis");
		service.update(id,title,content);
		response.sendRedirect("document.jspx");
		
		
		
	}
	
	/**
	 * 添加新资料
	 * */
	private void NewDocument(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String content = request.getParameter("content");
		String title = request.getParameter("title");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String projectid = (String) session.getAttribute("projectid");
		service.save(title,content,user,projectid);
		response.sendRedirect("document.jspx?method=toNewDocument&code=1002");
	}
	
	/**
	 * 进入到添加新资料页面
	 * */
	
	private void toNewDocument(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/new_document.jsp").forward(request, response);
		
	}
	/**
	 * 到 document_show.jsp页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void toDocumentShow(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		Document document = service.findDocument(id);
		List<Comment> commentlist = cs.findAllByDocumentid(id);
		request.setAttribute("commentlist",commentlist);
		request.setAttribute("document", document);
		request.getRequestDispatcher("WEB-INF/views/document_show.jsp").forward(request, response);
		
	}

	private void findAllDocument(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String p = request.getParameter("p");
		int pageNum =0;
		if(p == null || "".equals(p)){
			pageNum = 1;
		}else{
			pageNum = Integer.valueOf(p);
		}
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String id = (String) session.getAttribute("projectid");
		Page<DocumentComment> list = service.findAll(user,pageNum,id);
		request.setAttribute("DocuList", list);
		request.getRequestDispatcher("WEB-INF/views/document.jsp").forward(request, response);
		
		
		
	}
	
}
